package com.starblink.brand.ui.part

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.skydoves.balloon.Balloon
import com.starblink.android.basic.adapter.MutableAdapter
import com.starblink.android.basic.brv.DefaultDecoration
import com.starblink.android.basic.ext.color
import com.starblink.android.basic.ext.inflater
import com.starblink.android.basic.ext.string
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.extension.lazyOnNone
import com.starblink.basic.log.SKLogger
import com.starblink.brand.R
import com.starblink.brand.adapter.BrandSortCell
import com.starblink.brand.bean.SortBean
import com.starblink.brand.databinding.ItemBrandSortBinding
import com.starblink.search.config.SearchConfig

class PartSort (
    var context: Context,
    private var balloon:Balloon,
    private var doFilter:(sortBean:SortBean)->Unit){
    private val sorts = arrayListOf(
        SortBean(
            sort = SearchConfig.SORT_DEFAULT,
            select = true,
            text = "Default",
        ),
        SortBean(
            sort = SearchConfig.SORT_PRICE_DOWN,
            select = false,
            text = context.string(R.string.brand_price_to_low),
        ),
        SortBean(
            sort = SearchConfig.SORT_PRICE_UP,
            select = false,
            text = context.string(R.string.brand_price_to_high),
        ),
    )

    private val adapter by lazyOnNone {
        MutableAdapter(sorts).apply {
            addVhDelegate { viewGroup ->
                BrandSortCell(ItemBrandSortBinding.inflate(viewGroup.inflater, viewGroup, false)) { position ->
                    notifyList(position)
                    sorts.forEach { it.select=false }
                    sorts[position].select=true
                    notifyDataSetChanged()
                    balloon.dismiss()
                }
            }
        }
    }

    fun handleVM(){
        val rv_sort=balloon.getContentView().findViewById<RecyclerView>(R.id.rv_sort)
        rv_sort.addItemDecoration(DefaultDecoration(context).apply {
            startVisible = false
            endVisible = false
            this.setColor(context.color(com.starblink.basic.style.R.color.sk_gray100))
            this.setMargin(start = 16.dp2px(), end = 16.dp2px())
            this.setDivider(1)
        })
        rv_sort.adapter = adapter
        rv_sort.layoutManager = LinearLayoutManager(context)
    }

    private fun notifyList(position:Int){
        SKLogger.d("notifyList","PartSort --- $position")
        doFilter.invoke(sorts[position])
    }

}